//
//  StatusCell.swift
//  NSinaWeibo
//
//  Created by LoliQueen on 16/2/20.
//  Copyright © 2016年 LoliQueen. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    ///加载xib才使用的方法
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    var bottomConstraint : Constraint?
    //设置模型属性
    var status : Status?{
        didSet{
            StatusCellOriginaView.status = status
//            StatusRetweetedView.retweetedStatus = status?.retweeted_status
//            print(status?.pictureUrls)
            ///根据是是转发微博 ，来设置视图底部的约束
            self.bottomConstraint?.uninstall()
            if let re = status?.retweeted_status {
                //是转发微博
                //赋值
                StatusRetweetedView.retweetedStatus = re
                
                StatusCellBottomView.snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint =  make.top.equalTo(StatusRetweetedView.snp_bottom).offset(homeVCMargin).constraint
                }
                
                StatusRetweetedView.hidden = false
            }else{
                ///不是转发微博
                StatusCellBottomView.snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint =  make.top.equalTo(StatusCellOriginaView.snp_bottom).offset(homeVCMargin).constraint
                }
                StatusRetweetedView.hidden = true
            }
        }
    }
    ///默认的构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        //设置选中是cell间的分割线 为无
        selectionStyle = .None
    }
    
    ///设置子控件
    func setupUI() {
        contentView.addSubview(StatusCellOriginaView)
        contentView.addSubview(StatusCellBottomView)
        contentView.addSubview(StatusRetweetedView)
        
        //设置子视图的位置
        StatusCellOriginaView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
            //设置高度
            //            make.height.equalTo(250)
        }
        
        StatusCellBottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
           self.bottomConstraint = make.top.equalTo(StatusRetweetedView.snp_bottom).offset(homeVCMargin).constraint
            
            make.height.equalTo(40)
        }
        
        ///设置转发微博的部分视图
        StatusRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(StatusCellOriginaView.snp_bottom)
            make.left.equalTo(StatusCellOriginaView.snp_left)
            make.right.equalTo(StatusCellOriginaView.snp_right)
        }
        
        ///给contentView设置约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(StatusCellBottomView.snp_bottom)
        }
        
        
    }
    
    ///懒加载子视图
    lazy var StatusCellOriginaView : statusCellOriginaView = statusCellOriginaView()
    lazy var StatusCellBottomView : statusCellBottomView = statusCellBottomView()
    ///转发微博部分视图
    lazy var StatusRetweetedView : statusRetweetedView = statusRetweetedView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
